[20220810_173256_user] D2_main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. * @attention
  7. *
  8. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  9. * All rights reserved.</center></h2>
  10. *
  11. * This software component is licensed by ST under BSD 3-Clause license,
  12. * the License; You may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at:
  14. * www.st.com/SLA0044
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "i2c.h"
  24. #include "tim.h"
  25. #include "stm32f4xx_hal.h"
  26. #include "gpio.h"
  27. #include "stdio.h"
  28. /* Private variables ---------------------------------------------------------*/
  29. /* Private variables ---------------------------------------------------------*/
  30. #define VECT_TAB_OFFSET 0x10000
  31. int tankstatus = 0;
  32. uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
  33. uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
  34. uint8_t zigbeestatu[20];
  35. uint8_t zigbeestatu1[5];
  36. uint8_t zigbeecon[7]="connect";
  37. uint8_t zigbeenotcon[11]="not connect";
  38. uint8_t zigbeechang[14]="change is down";
  39. int M9_WATERLEVEL =0;
  40. /* USER CODE BEGIN PV */
  41. typedef void (*pFunction)(void);
  42. /* USER CODE END PV */
  43. /* Private function prototypes -----------------------------------------------*/
  44. void SystemClock_Config(void);
  45. void MX_ADC1_Init1(char pin);
  46. void sensor(void);
  47. /* USER CODE BEGIN PFP */
  48. /* USER CODE END PFP */
  49. /* Private user code ---------------------------------------------------------*/
  50. /* USER CODE BEGIN 0 */
  51. int _write(int fd, char *ch, int len)
  52. {
  53. HAL_UART_Transmit(&huart4, (uint8_t*)ch, len, 0xFFFF);
  54. HAL_UART_Transmit(&huart2, (uint8_t*)ch, len, 0xFFFF);
  55. return len;
  56. }
  57. void print(char* s, float c)
  58. { int w;
  59. int w_d;
  60. int w_f;
  61. if(c<0)
  62. {
  63. w = c*100;
  64. w_d = w/100;
  65. w_f = (-1*w)%100;
  66. }
  67. else
  68. {
  69. w = c*100;
  70. w_d = w/100;
  71. w_f = w%100;
  72. }
  73. printf("%s = %d.%d\n",s,w_d,w_f);
  74. }
  75. /* USER CODE END 0 */
  76. /**
  77. * @brief The application entry point.
  78. * @retval int
  79. */
  80. int main(void)
  81. {
  82. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  83. HAL_Init();
  84. SystemClock_Config();
  85. MX_GPIO_Init();
  86. MX_USART2_UART_Init();
  87. MX_UART4_Init();
  88. MX_USART1_UART_Init();
  89. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
  90. HAL_Delay(1000);
  91. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
  92. HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
  93. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
  94. if (zigbeestatu[18]== 0x01 || zigbeestatu[18]== 0x02)
  95. {
  96. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
  97. }
  98. else if(zigbeestatu[18]== 0x05)
  99. {
  100. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
  101. }
  102. while (1)
  103. {
  104. sensor();
  105. if(M9_WATERLEVEL<5)
  106. {
  107. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10, GPIO_PIN_RESET);
  108. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);
  109. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);
  110. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
  111. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);
  112. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
  113. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  114. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);
  115. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
  116. }
  117. }
  118. }
  119. void SystemClock_Config(void)
  120. {
  121. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  122. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  123. __HAL_RCC_PWR_CLK_ENABLE();
  124. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  125. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  126. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  127. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  128. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  129. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  130. RCC_OscInitStruct.PLL.PLLM = 8;
  131. RCC_OscInitStruct.PLL.PLLN = 72;
  132. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  133. RCC_OscInitStruct.PLL.PLLQ = 3;
  134. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  135. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  136. {
  137. Error_Handler();
  138. }
  139. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  140. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  141. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  142. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  143. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  144. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  145. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149. }
  150. void user_pwm_setvalue(uint16_t value)
  151. {
  152. TIM_OC_InitTypeDef sConfigOC;
  153. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  154. sConfigOC.Pulse = value;
  155. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  156. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  157. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
  158. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  159. }
  160. void user_pwm_setvalue2(uint16_t value)
  161. {
  162. TIM_OC_InitTypeDef sConfigOC;
  163. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  164. sConfigOC.Pulse = value;
  165. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  166. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  167. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  168. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  169. }
  170. void sensor(void)
  171. {
  172. MX_GPIO_Input1(GPIO_PIN_9);
  173. if (HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_9) == GPIO_PIN_SET)
  174. {
  175. M9_WATERLEVEL =1;
  176. HAL_UART_Transmit(&huart4,(unsigned char*)&M9_WATERLEVEL,1,10);
  177. }
  178. else
  179. {
  180. M9_WATERLEVEL =0;
  181. print("M9_WATERLEVEL",M9_WATERLEVEL);
  182. }
  183. HAL_Delay(10000);
  184. }
  185. void MX_ADC1_Init1(char pin)
  186. {
  187. ADC_ChannelConfTypeDef sConfig = {0};
  188. hadc1.Instance = ADC1;
  189. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  190. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  191. hadc1.Init.ScanConvMode = DISABLE;
  192. hadc1.Init.ContinuousConvMode = DISABLE;
  193. hadc1.Init.DiscontinuousConvMode = DISABLE;
  194. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  195. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  196. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  197. hadc1.Init.NbrOfConversion = 1;
  198. hadc1.Init.DMAContinuousRequests = DISABLE;
  199. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  200. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  201. {
  202. Error_Handler();
  203. }
  204. sConfig.Channel = pin;
  205. sConfig.Rank = 1;
  206. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  207. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  208. {
  209. Error_Handler();
  210. }
  211. }
  212. void MX_GPIO_Input1(unsigned long pin)
  213. {
  214. GPIO_InitTypeDef GPIO_InitStruct = {0};
  215. __HAL_RCC_GPIOE_CLK_ENABLE();
  216. GPIO_InitStruct.Pin = pin;
  217. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  218. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  219. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  220. }
  221. void Error_Handler(void)
  222. {
  223. }